<template>
    <div>
        <Card>
            <!-- search start -->
            <div class="search">
                <!-- <div class="add">
                    <Button size="default" icon="md-add" type="primary" @click="addPage">新增商品</Button>
                </div> -->
                <Form :model="search" class="search_box" inline label-position="right">
                    <FormItem label="分部 -> 楼层：">
                        <Cascader class="cascader" :value="cascadeArr" :data="cascadeList" change-on-select @on-change="cascadeSelection" :clearable="false"></Cascader>
                    </FormItem>

                    <FormItem label="巡店类型：">
                        <Select class="option" v-model="search.cp_type">
                            <Option :value="-1">全部</Option>
                            <Option :value="1">社区巡店</Option>
                            <Option :value="2">日常巡店</Option>
                        </Select>
                    </FormItem>

                    <FormItem label="时间：">
                        <DatePicker style="width:300px" @on-change="changeTime" type="datetimerange"></DatePicker>
                    </FormItem>

                    <FormItem label="巡店人：">
                        <Input v-model="search.username"></Input>
                    </FormItem>

                    <FormItem>
                        <Button icon="md-search" type="primary" @click="searchList">搜索</Button>
                    </FormItem>
                </Form>

            </div> <!-- end search -->

            <Table ref="table" :loading="loading" stripe :columns="columns1" :data="data1"></Table>
            <Page :total="total" :current="search.page" show-sizer show-total :page-size="search.rows" @on-page-size-change="getListByPageSize" @on-change="getListByPageNum"> </Page>
        </Card>

    </div>
</template>
<script>
    import {
        mapMutations,
        mapGetters
    } from 'vuex'
    import axios from '@/libs/api.request'
    export default {
        name: 'check-log',
        data() {
            return {
                // 级联回显数组
                cascadeArr: [-1],

                loading: false,

                // 数据总条数
                total: 0,

                // 搜索对象
                search: {
                    branchid: -1,
                    floorid: -1,
                    cp_type: -1,
                    username: '',
                    start_time: '',
                    end_time: '',
                    page: 1,
                    rows: 10
                },

                columns1: [{
                        type: 'index',
                        align: 'center',
                        width: 60
                    },
                    {
                        title: '分部名称',
                        align: 'center',
                        key: 'branch_zw'
                    },
                    {
                        title: '楼层',
                        align: 'center',
                        key: 'floor_zw'
                    },
                    {
                        title: '巡店类型',
                        align: 'center',
                        key: 'type_zw'

                    },
                    {
                        title: '巡店人',
                        align: 'center',
                        key: 'username'

                    },
                    {
                        title: '巡店结束时间',
                        align: 'center',
                        key: 'end_time'
                    },
                    {
                        title: '操作',
                        align: 'center',
                        render: (h, params) => {
                            return h('div', [
                                h('Button', {
                                    props: {
                                        type: 'primary',
                                        size: 'default',
                                    },
                                    style: {
                                        marginRight: '14px',
                                        width: '80px',
                                        height: '30px'
                                    },
                                    on: {
                                        click: () => {
                                            this.myClose('log-details')
                                            this.$_changePage('log-details', '巡店详情', {
                                                id: params.row.id
                                            })
                                        }
                                    }
                                }, '查看详情'),
                            ])
                        }
                    },
                ],
                data1: []

            }
        },
        methods: {
            ...mapMutations(['myClose']),
            ...mapGetters(['getBranchList']),

            // 获取巡店日志列表
            getCheckLogList() {
                this.loading = true
                let {
                    ...data
                } = this.search
                axios.ajax_post('admin-patrol_record-ajax_get_pr', data).then(res => {
                    this.loading = false
                    if (res.data.code === 1) {
                        this.data1 = res.data.data.list
                        this.total = res.data.data.total_num

                        if (this.data1.length < 1 && this.total > 0) {
                            this.search.page = Math.ceil(this.total / this.search.rows)
                            this.getCheckLogList()
                        }

                    } else {
                        this.$Notice.error({
                            title: res.data.msg,
                            duration: 3
                        })
                    }
                })
            },

            // 级联选择
            cascadeSelection(value) {
                if (value.length == 1) {
                    this.search.branchid = value[0]
                    this.search.floorid = -1
                    return false
                }
                this.search.branchid = value[0]
                this.search.floorid = value[1]
            },

            // 格式化搜索时间
            changeTime(value) {
                this.search.start_time = value[0]
                this.search.end_time = value[1]
            },
            // 搜索
            searchList() {
                this.search.page = 1
                this.search.rows = 10
                this.getCheckLogList()
            },

            getListByPageSize(rows) {
                this.search.rows = rows
                this.getCheckLogList()
            },

            getListByPageNum(page) {
                this.search.page = page
                this.getCheckLogList()
            },
        },
        computed: {
            cascadeList() {
                let list = JSON.parse(JSON.stringify(this.getBranchList()))
                list.forEach((item) => {
                    item.children.forEach((floor) => {
                        delete floor.children
                    })
                })
                list.unshift({
                    value: -1,
                    label: '全部'
                })
                return list
            },
        },
        created() {
            this.getCheckLogList()
        }
    }
</script>
<style lang="less" scoped>
    .search {
        .search_box {
            display: flex;

            .option {
                width: 120px;
            }

            .ivu-form-item {
                margin-right: 12px;
                display: flex;
                justify-content: flex-start;
                margin-bottom: 20px;
            }
        }
    }
</style>